/** * Created by rubin on 2020/6/4. */

<template>
  <main>
    <bg />
    <div class="login-content">
      <div class="content">
        <div class="form sign-in">
          <div v-if="currentStep === 0">
            <h2>{{ title }} 校验用户名</h2>
            <label :class="focus.username || formData.username ? 'on' : ''">
              <div class="placeholder">
                <span>
                  <span>用</span>
                  <span>户</span>
                  <span>名</span>
                </span>
              </div>
              <i class="el-icon-user"></i>
              <input
                type="text"
                v-model="formData.username"
                ref="username"
                @focus.self="focus.username = true"
                @blur.self="focus.username = formData.username ? true : false"
              />
            </label>
          </div>
          <div v-if="currentStep === 1">
            <h2>{{ title }} 校验密保答案</h2>
            <label :class="focus.username || formData.username ? 'on' : ''">
              <div class="placeholder">
                <span>
                  <span>您</span>
                  <span>的</span>
                  <span>密</span>
                  <span>保</span>
                  <span>问</span>
                  <span>题</span>
                  <span>为</span>
                </span>
              </div>
              <i class="el-icon-lock"></i>
              <input
                type="text"
                v-model="question"
                ref="question"
                :disabled="true"
              />
            </label>
            <label :class="focus.username || formData.username ? 'on' : ''">
              <div class="placeholder">
                <span>
                  <span>您</span>
                  <span>的</span>
                  <span>密</span>
                  <span>保</span>
                  <span>答</span>
                  <span>案</span>
                  <span>为</span>
                </span>
              </div>
              <i class="el-icon-lock"></i>
              <input
                type="text"
                v-model="formData.answer"
                ref="answer"
                @focus.self="focus.answer = true"
                @blur.self="focus.answer = formData.answer ? true : false"
              />
            </label>
          </div>
          <div v-if="currentStep === 2">
            <h2>{{ title }} 重置密码</h2>
            <label :class="focus.username || formData.username ? 'on' : ''">
              <div class="placeholder">
                <span>
                  <span>新</span>
                  <span>密</span>
                  <span>码</span>
                </span>
              </div>
              <i class="el-icon-lock"></i>
              <input
                type="text"
                v-model="formData.newPassword"
                ref="newPassword"
                @focus.self="focus.newPassword = true"
                @blur.self="
                  focus.newPassword = formData.newPassword ? true : false
                "
              />
            </label>
            <label :class="focus.username || formData.username ? 'on' : ''">
              <div class="placeholder">
                <span>
                  <span>确</span>
                  <span>认</span>
                  <span>密</span>
                  <span>码</span>
                </span>
              </div>
              <i class="el-icon-lock"></i>
              <input
                type="text"
                v-model="formData.reNewPassword"
                ref="reNewPassword"
                @focus.self="focus.reNewPassword = true"
                @blur.self="
                  focus.reNewPassword = formData.reNewPassword ? true : false
                "
              />
            </label>
          </div>
          <button
            :loading="loading"
            @click="doNext"
            type="button"
            class="styled-button"
          >
            <span class="styled-button__real-text-holder">
              <span class="styled-button__real-text">{{ btnText }}</span>
              <span class="styled-button__moving-block face">
                <span class="styled-button__text-holder">
                  <span class="styled-button__text">{{ btnText }}</span>
                </span> </span
              ><span class="styled-button__moving-block back">
                <span class="styled-button__text-holder">
                  <span class="styled-button__text">{{ btnText }}</span>
                </span>
              </span>
            </span>
          </button>
          <div class="sub-cont" @click="goLogin">
            <span>返回登录</span>
          </div>
        </div>
      </div>
      <div style="display: none" id="cube-template">
        <div class="cube">
          <div class="shadow"></div>
          <div class="sides">
            <div class="back"></div>
            <div class="top"></div>
            <div class="left"></div>
            <div class="front"></div>
            <div class="right"></div>
            <div class="bottom"></div>
          </div>
        </div>
      </div>
    </div>
  </main>
</template>

<script>
import userService from '../../api/user'
import panUtil from '../../utils/common'
import bg from '@/layout/bg'
export default {
  data() {
    return {
      currentStep: 0,
      username: '',
      question: '',
      answer: '',
      newPassword: '',
      reNewPassword: '',
      token: '',
      processStatus: 'process',
      title: import.meta.env.VITE_TITLE,
      btnText: '下一步',
      loading: false,
      formData: {
        username: '',
        answer: '',
        newPassword: '',
        reNewPassword: '',
      },
      focus: {
        username: false,
        answer: false,
        newPassword: false,
        reNewPassword: false,
      },
    }
  },
  components: {
    bg,
  },
  methods: {
    goLogin() {
      this.$router.push({ name: 'Login' })
    },
    doNext() {
      if (this.currentStep === 0) {
        this.btnText = '下一步'
        this.goStepTwo()
      } else if (this.currentStep === 1) {
        this.btnText = '下一步'
        this.goStepThree()
      } else if (this.currentStep === 2) {
        this.resetPassword()
      }
    },
    goStepOne() {
      let _this = this
      _this.loading = true
      _this.currentStep = 0
      _this.username = ''
      _this.question = ''
      _this.answer = ''
      _this.newPassword = ''
      _this.reNewPassword = ''
      _this.token = ''
      _this.processStatus = 'process'
      _this.$nextTick(function () {
        _this.$refs.username.focus()
      })
      _this.loading = false
    },
    goStepTwo() {
      let _this = this
      if (!_this.formData.username) {
        _this.$message.error('请输入要修改密码的用户名')
        return
      }
      _this.loading = true
      userService.checkUsername(
        {
          username: _this.formData.username,
        },
        (res) => {
          _this.question = res.data
          _this.currentStep = 1
          _this.$nextTick(function () {
            _this.$refs.answer.focus()
            _this.loading = false
          })
        },
        (res) => {
          _this.$message.error(res.message)
          _this.loading = false
        }
      )
    },
    goStepThree() {
      let _this = this
      if (!_this.formData.answer) {
        _this.$message.error('请输入密保答案')
        return
      }
      _this.loading = true
      userService.checkAnswer(
        {
          username: _this.formData.username,
          question: _this.question,
          answer: _this.formData.answer,
        },
        (res) => {
          _this.token = res.data
          _this.currentStep = 2
          _this.btnText = '提 交'
          _this.$nextTick(function () {
            _this.$refs.newPassword.focus()
            _this.loading = false
          })
        },
        (res) => {
          _this.$message.error(res.message)
          _this.loading = false
        }
      )
    },
    resetPassword() {
      let _this = this
      if (!_this.token) {
        _this.$message.error('由于您长时间未操作，将返回步骤一重新申请密码重置')
        _this.goStepOne()
        return
      }
      if (_this.checkPassword()) {
        _this.loading = true
        userService.resetPassword(
          {
            username: _this.formData.username,
            newPassword: _this.formData.newPassword,
            token: _this.token,
          },
          () => {
            _this.loading = false
            _this.processStatus = 'success'
            _this.$message.success('密码重置成功！即将跳转至登录页面')
            setTimeout(_this.goLogin, 1000)
          },
          (res) => {
            if (res.status === 2) {
              _this.loading = false
              _this.$message.error(
                '由于您长时间未操作，将返回步骤一重新申请密码重置'
              )
              _this.goStepOne()
            } else {
              _this.loading = false
              _this.$message.error(res.message)
            }
          }
        )
      }
    },
    checkPassword() {
      if (!panUtil.checkPassword(this.formData.newPassword)) {
        this.$message.error('请输入8-16位的密码')
        return false
      }
      if (
        !this.formData.reNewPassword ||
        this.formData.newPassword !== this.formData.reNewPassword
      ) {
        this.$message.error('两次密码输入不一致')
        return false
      }
      return true
    },
  },
  mounted() {
    this.$refs.username.focus()
  },
  watch: {},
}
</script>

<style lang="scss" scoped>
*,
*:before,
*:after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

input {
  border: none;
  outline: none;
  background: none;
  font-family: 'Open Sans', Helvetica, Arial, sans-serif;
}
svg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
}
.login-content {
  min-height: 100vh;
  .content {
    overflow: hidden;
    position: absolute;
    left: 50%;
    top: 50%;
    // width: 900px;
    // height: 550px;
    // margin: -300px 0 0 -450px;
    transform: translate(-50%, -50%);
    background: #fff;
    .form {
      position: relative;
      // width: 640px;
      height: 100%;
      transition: -webkit-transform 0.6s ease-in-out;
      transition: transform 0.6s ease-in-out;
      transition: transform 0.6s ease-in-out, -webkit-transform 0.6s ease-in-out;
      padding: 50px;

      h2 {
        width: 100%;
        font-size: 26px;
        text-align: center;
      }

      label {
        display: block;
        width: 260px;
        margin: 25px auto 0;
        border: 1px solid rgba(0, 0, 0, 0.4);
        border-radius: 3px;
        position: relative;
        i {
          position: absolute;
          left: 16px;
          top: 50%;
          visibility: visible;
          transform: translateY(-50%) scale(1);
          transition: all 0.4s;
        }
        .placeholder {
          position: absolute;
          left: 0;
          top: 50%;
          height: 100%;
          transform: translateY(-50%);
          padding-left: 45px;
          display: flex;
          align-items: center;
          & > span {
            display: inline-block;
            perspective: 500px;
            span {
              display: inline-block;
              font-size: 14px;
              color: #909399;
              animation: letterAnimOut 0.5s ease forwards;

              // @for $i from 1 through 3 {
              //   &:nth-child(#{$i}) {
              //     animation-delay: $i * 0.0.5s;
              //   }
              // }
            }
          }
        }

        &.on {
          i {
            transform: translateY(-50%) scale(0.1);
            visibility: hidden;
            opacity: 0;
          }
          .placeholder {
            span {
              span {
                animation: letterAnimIn 0.5s ease forwards;
                // transform: translate(-30px, -21px);
                // text-shadow: 0 0 5px;
                @for $i from 1 through 3 {
                  &:nth-child(#{$i}) {
                    animation-delay: $i * 0.1s;
                  }
                }
              }
            }
          }
        }

        input {
          display: block;
          width: 100%;
          // margin-top: 5px;
          padding: 12px 15px;
          font-size: 16px;
          // border-bottom: 1px solid rgba(0, 0, 0, 0.4);
        }
      }
    }
  }
}
@keyframes letterAnimIn {
  0% {
    -webkit-transform: translate(0, 0);
    transform: translate(0, 0);
  }
  25% {
    -webkit-transform: translate(0, 10px);
    transform: translate(0, 10px);
    color: red;
  }
  45% {
    -webkit-transform: translate(0, 10px);
    transform: translate(0, 10px);
    opacity: 0;
    color: red;
  }
  55% {
    -webkit-transform: translate(0, 10px);
    transform: translate(0, 10px);
    opacity: 0;
  }
  56% {
    -webkit-transform: translate(-30px, -21px);
    transform: translate(-30px, -21px);
    opacity: 0;
    color: #00ff6b;
  }
  76% {
    color: #00ff6b;
    opacity: 1;
    -webkit-transform: translate(-30px, -21px);
    transform: translate(-30px, -21px);
  }
  100% {
    -webkit-transform: translate(-30px, -21px);
    transform: translate(-30px, -21px);
    background-color: #fff;
    opacity: 1;
  }
}
@keyframes letterAnimOut {
  0% {
    -webkit-transform: translate(-30px, -21px);
    transform: translate(-30px, -21px);
    opacity: 1;
  }
  25% {
    -webkit-transform: translate(-30px, -40px);
    transform: translate(-30px, -40px);
    opacity: 0;
  }
  45% {
    -webkit-transform: translate(0, 10px);
    transform: translate(0, 10px);
    opacity: 0;
  }
  55% {
    -webkit-transform: translate(0, 10px);
    transform: translate(0, 10px);
    opacity: 0;
    color: red;
  }
  56% {
    -webkit-transform: translate(0, 10px);
    transform: translate(0, 10px);
    color: red;
  }
  100% {
    -webkit-transform: translate(0, 0);
    transform: translate(0, 0);
  }
}

.styled-button {
  appearance: none;
  user-select: none;
  cursor: pointer;
  font-size: 14px;
  width: 100%;
  padding: 15px;
  outline: none;
  background: none;
  position: relative;
  color: rgba($color: #fff, $alpha: 0.8);
  border-radius: 3px;
  margin-bottom: 25px;
  border: none;
  text-transform: uppercase;
  font-weight: 700;
  letter-spacing: 0.1em;
  background: #65c6cf;
  -webkit-transition: all 0.3s ease;
  transition: all 0.3s ease;
  overflow: hidden;
  margin-top: 20px;
}

.styled-button__real-text {
  color: transparent;
  display: inline-block;
}
.styled-button__text-holder {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 100%;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-transition: all 0.3s ease;
  transition: all 0.3s ease;
}
.styled-button__moving-block {
  -webkit-transition: all 0.3s ease;
  transition: all 0.3s ease;
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
}
.styled-button__moving-block.back {
  color: white;
  -webkit-transform: translateX(-100%);
  transform: translateX(-100%);
}
.styled-button__moving-block.back .styled-button__text-holder {
  -webkit-transform: translateX(100%);
  transform: translateX(100%);
}
.styled-button:hover,
.styled-button:active {
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
  background: #65c6cf;
}
.styled-button:hover .face,
.styled-button:active .face {
  -webkit-transform: translateX(100%);
  transform: translateX(100%);
}
.styled-button:hover .face .styled-button__text-holder,
.styled-button:active .face .styled-button__text-holder {
  -webkit-transform: translateX(-100%);
  transform: translateX(-100%);
}
.styled-button:hover .back,
.styled-button:active .back {
  -webkit-transform: translateX(0);
  transform: translateX(0);
}
.styled-button:hover .back .styled-button__text-holder,
.styled-button:active .back .styled-button__text-holder {
  -webkit-transform: translateX(0);
  transform: translateX(0);
}
.styled-button:active {
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
}

.forgot-pass {
  margin-top: 15px;
  text-align: right;
  font-size: 12px;
  a {
    color: #999;
  }
}
.sub-cont {
  font-size: 12px;
  color: #999;
  text-align: center;
  cursor: pointer;
  span {
    color: #65c6cf;
  }
}
</style>
